﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Threading;
using QLKS.SysLib;
using System.Data;

namespace QLKS.Templates
{
    public class FormTrans: Window
    {
        #region PROPERTY
        object parent;
        static public string M_LAN = string.Empty;
        DataSet ds = new DataSet();
        public bool EnterAsTab = true;
        #endregion
        //Khởi tạo mặc định
        #region KHỞI TẠO
        public FormTrans()
            : base()
        {
            parent = this;
            M_LAN = LanguageLib.GetLanguage();
            this.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(FormTrans_PreviewKeyDown);
            this.Loaded += new RoutedEventHandler(FormTrans_Loaded);
            this.PreviewKeyDown+=new KeyEventHandler(FormTrans_KeyDown);
        }
        #endregion
        //Form Load
        #region FormLoad
        private void FormTrans_Loaded(object sender, RoutedEventArgs e)
        {
            this.Reload_Language();
        }
        #endregion
        //KeyDown{F3}
        #region KeyDown{F12}
        private void FormTrans_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F12)
            {
                LanguageLib.ScanControl(this.Name,"",this);
                DataSet ds = SysObj.Preview_F12(this.Name);
                if(ds.Tables.Count > 0)
                    LanguageLib.LoadControl(ds.Tables[0],"",this, LanguageLib.GetLanguage());
            }
        }
        #endregion

        #region KeyDown{Enter}
        protected void FormTrans_KeyDown(object sender, KeyEventArgs e)
        {
            if (!EnterAsTab)
                return;
            if (e.Key == Key.Enter /*|| e.Key == Key.Down || e.Key == Key.Up*/)
            {
                //Type t = Keyboard.FocusedElement.GetType();
                object t = e.OriginalSource;
                //virtual v = ( t as Visual);
                
                if ((t as Button) != null && e.Key == Key.Enter)
                    return;
                if ((t as ComboBox) != null && e.Key != Key.Enter)
                    return;
                if ((t as Infragistics.Windows.DataPresenter.DataRecordCellArea) != null)
                    return;
                if((t as Infragistics.Windows.DataPresenter.CellValuePresenter)!=null)
                    return;
                //if (e.Key == Key.Tab)
                //    return;
                //this.Dispatcher.BeginInvoke(new Action(() =>
                //{
                //    WinAPISenkey.SenKey(ModifierKeys.Control, Key.Tab);
                //}), DispatcherPriority.Background);
                TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);//new TraversalRequest(e.Key == Key.Up ? FocusNavigationDirection.Previous : FocusNavigationDirection.Next);
                UIElement keyboardFocus = Keyboard.FocusedElement as UIElement;
                if (keyboardFocus != null)
                {
                    keyboardFocus.MoveFocus(tRequest);
                }
                    e.Handled = true;
            }
        }
        #endregion
        //Refresh Language
        #region Refresh Language
        public void Reload_Language()
        {
            M_LAN = LanguageLib.GetLanguage();
            string FileName = System.Windows.Forms.Application.StartupPath + "\\LANG\\" + this.Name + ".xml";
            if (!System.IO.File.Exists(FileName))
                return;
            ds.ReadXml(FileName);
            LanguageLib.LoadControl(ds.Tables[0],"",parent, M_LAN);
        }
        #endregion

        //Show ProgressBar for Waiting
        #region Show ProgressBar
        public void Waiting(long maxvalue, long value)
        {
            Waiting(maxvalue, value);
        }
        #endregion
    }
}
